<?php
/**
 * 公文管理控制器
 * 
 * @category QuickBug
 * @copyright http://www.vquickbug.com
 */
class DocumentController extends BaseController
{
	/**
	 * 模型
	 *
	 * @var unknown_type
	 */
	private $model;
	
	/**
	 * 自动运行
	 */
	public function init(){
		parent::init();
		if(!$this->is_operator){
			$this->msgbox('您无权限访问',url('index','index'));
		} 
		$this->model = new Model_Document();
		
	}
	
	public function indexAction(){
		$this->view->controller = $this->controller;
	}
	
	public function listAction(){
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$offset = ($page-1)*$rows;
		$result = array();
		$result["total"] = $this->model->countDocument();
		$result["rows"] = $this->model->listDocument($offset,$rows);
		
		echo json_encode($result);
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function addAction(){
		$param = array(
			'name'=>$this->request->getPost('name'),
			'title'=>$this->request->getPost('title'),
			'office'=>$this->request->getPost('office'),
			'content'=>$this->request->getPost('content'),
			'basic_opinion'=>$this->request->getPost('basic_opinion'),
			'leader_opinion'=>$this->request->getPost('leader_opinion'),
			'date'=>date("Y-m-d"),
		);
		
		$result = $this->model->addDocument($param);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'添加记录失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function updateAction(){
		$id = intval($this->request->getGet('id'))?intval($this->request->getGet('id')):0;
		$param = array(
			'name'=>$this->request->getPost('name'),
			'title'=>$this->request->getPost('title'),
			'office'=>$this->request->getPost('office'),
			'content'=>$this->request->getPost('content'),
			'basic_opinion'=>$this->request->getPost('basic_opinion'),
			'leader_opinion'=>$this->request->getPost('leader_opinion'),
		);

		$result = $this->model->updateDocument($id,$param);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'编辑记录失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
	
	public function delAction(){
		$id = intval($this->request->getPost('id'))?intval($this->request->getPost('id')):0;
		$result = $this->model->delDocument($id);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'撤销记录失败'));
		}
		
		QP_Layout::stop();
		$this->setViewAutoRender(false);
	}
}